package com.crm.daoImpl;

import com.crm.dao.UserDao;
import com.crm.pojo.User;
import com.crm.util.jdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @version : 1.0
 * @auther : hjx
 * @Date : 2021/6/26
 * @Description : 实训
 */
public class UserDaoImpl implements UserDao {

    @Override
    public List<User> login(String username, String password) {
        Connection con = jdbcUtils.getCon();
        String sql = "SELECT * from sys_user WHERE user_name = ? and user_password = ?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<User> list = new ArrayList<User>();
        try {

            ps = con.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            rs = ps.executeQuery();
            while (rs.next()) {
                int userId = rs.getInt(1);
                String name = rs.getString(2);
                String pwd = rs.getString(3);
                String state = rs.getString(4);
                list.add(new User(userId,name,pwd,state));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            jdbcUtils.close(con,ps,rs);
        }
        return list;
    }

    @Override
    public void registry(User user) {

    }

    @Override
    public List<String> queryAllUser() {
        Connection conn = null;
        String sql = "select user_id,user_name from sys_user";
        conn = jdbcUtils.getCon();
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<String> list = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                list.add(id + "-" + name);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
